package utils;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ctf.network.ProtocolError;

/**
 * Class to encode and decode strings for position of trees in the game
 * @author N. Bhatia, A.Butt, N, Evans, C. Levesque, P. OKeefe
 *
 */
public class Trees_DataPackage implements IDataPackage {
	public Collection<Point2D> trees;

	@Override
	public void decode(String encodedData) throws ProtocolError {
		trees = new ArrayList<Point2D>();
		String treeString = encodedData.substring(1, encodedData.length() - 1);
		String[] treePoints = treeString.split(" ");
		for (int i = 0; i < treePoints.length; i++) {
			Point2D_DataPackage pointdp = new Point2D_DataPackage();
			pointdp.decode(treePoints[i]);
			trees.add(pointdp.point);
		}
	}

	@Override
	public String encode() {
		String encoded = new String("trees: ");
		Iterator<Point2D> iter = trees.iterator();
		while (iter.hasNext()) {
			Point2D_DataPackage pointdp = new Point2D_DataPackage();
			pointdp.point = iter.next();
			encoded += pointdp.encode()  + " ";
		}
		return encoded;
	}

}
